<?php
$link = mysql_connect('localhost', 'root', '');
//$link = mysql_connect('aecabill.accountsupportmysql.com', 'aeca', 'fArC5YyyG8NT9wdC5LXP');
if (!$link) { 
     die('There appeared to be an error while connecting to the website database: ' . mysql_error()); 
}
mysql_select_db(aeca);

$rtID = $_POST['asrid'];
$query = "select * from tbl_rate_type where rt_category='ASR'";
$result = mysql_query($query);

$cquery = "select * from  tbl_companies";
$cresult = mysql_query($cquery);

if($rtID){
$asr_query = "select * from tbl_asr_rates where asr_rate_id = $rtID";
$asr_result = mysql_fetch_array(mysql_query($asr_query));
}

if($_POST['asr_rate'] == 'asr_rate')
{
extract($_POST);
if($rtID){
$update_query = "UPDATE tbl_asr_rates SET c_nickname='$c_nickname',asr_rate_amount='$asr_rate_amount'," .
		"rt_rate_type='$rt_rate_type',asr_rate_eff_start_date='$asr_rate_eff_start_date',asr_rate_eff_end_date='$asr_rate_eff_end_date' WHERE asr_rate_id=$rtID";

if(mysql_query($update_query)){?>
<script>
$(document).ready(function(){
$("#success").text("ASR Rates Updated Successfully!").show();
return false;
});
</script>
<?}
}else{
	$insert_query = "INSERT INTO tbl_asr_rates (c_nickname,asr_rate_amount,rt_rate_type,asr_rate_eff_start_date,asr_rate_eff_end_date) " .
			"VALUES ('$c_nickname','$asr_rate_amount','$rt_rate_type','$asr_rate_eff_start_date','$asr_rate_eff_end_date');";
	if(mysql_query($insert_query))
	{?>
	<script>
	$(document).ready(function(){
		$("#success").text("ASR Rates Inserted Successfully!").show();
		return false;
	});
	</script>	
	<?}
}
if($rtID){
$asr_query = "select * from tbl_asr_rates where asr_rate_id = $rtID";
$asr_result = mysql_fetch_array(mysql_query($asr_query));
}
}
?>
<script>
  $(document).ready(function(){
    $("#asr_frm").validate();
  });
    $(function() {  
   $(".asrsubmit").click(function() { 
     // validate and process form here
     $("#asr_frm").validate();
     var name = 'asr';
     var asrid = $("#asrid").val();
     var c_nickname = $("#c_nickname").val();
     var rt_rate_type = $("#rt_rate_type").val();
     var asr_rate_amount = $("#asr_rate_amount").val();
     var asr_rate_eff_start_date = $(".asr_rate_eff_start_date").val();
     var asr_rate_eff_end_date = $(".asr_rate_eff_end_date").val();
     if(asr_rate_eff_start_date != '' && asr_rate_eff_end_date != ''){
	     $.post("/aec1/pages/update_cprofile.php",{name:name,asrid:asrid,c_nickname:c_nickname,rt_rate_type:rt_rate_type,asr_rate_amount:asr_rate_amount,asr_rate_eff_start_date:asr_rate_eff_start_date,asr_rate_eff_end_date:asr_rate_eff_end_date},function(data){
	     	if(data == 1){
	     	$( "#dialog:ui-dialog" ).dialog( "destroy" );
				$( "#dialog-message_u" ).dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog( "close" );
						window.location = "index.php?page=company-mang";
					}
				}
				});
	     	}else if(data == 2){
	     		$( "#dialog:ui-dialog" ).dialog( "destroy" );
				$( "#dialog-message_i" ).dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog( "close" );
						window.location = "index.php?page=company-mang#tabs-3";
					}
				}
				});
	     	}
	     });
	     return false;
     }
   });  
 });
</script>
<script>
var dates = $( "#from, #to" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" );
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
</script>
<!-- ui-dialog -->
<div id="dialog-message_u" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The ASR Rates has been Updated Successfully.
	</p>
</div>
<div id="dialog-message_i" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The new ASR Rates has been created.
	</p>
</div>

<span id='error'></span><span id='success'></span>
<fieldset>
<legend><strong><h3>ASR Rates</h3></strong></legend>
<div>
	<!-- <form action="index.php?page=asr-rates<?if($rtID){?>&Id=<?=$rtID;}?>" method="post" id="asr_frm"> -->
	<form action="" method="post" id="asr_frm">
		<table>
		<tr>
			<td><label><strong>LEC Nickname</strong></label>:</td>
			<td><select name="c_nickname" id="c_nickname">
			<? while($crow = mysql_fetch_array($cresult)){?>
				<option value=<?=$crow['c_nickname'];?>
				<? if($asr_result['c_nickname'] == $crow['c_nickname']){ echo 'selected="selected"'; } else {} ?>
				><?=$crow['c_nickname'];?></option>
			<?}?>
									</select></td>
		</tr>
		<tr>
			<td><label><strong>Rate Type</strong></label>:</td>
			<td>
				<select name="rt_rate_type" id="rt_rate_type">
				<? while($asr = mysql_fetch_array($result)){?>
					<option value="<?=$asr['rt_rate_type'];?>"
					<? if($asr_result['rt_rate_type'] == $asr['rt_rate_type']){ echo 'selected="selected"'; } else {} ?>
					><? echo $asr['rt_rate_type'];?></option>
				<?}?>
				</select>
			</td>
		</tr>
		<tr>
			<td><strong>Rate* :</strong></td>
			<td><input type="text" name="asr_rate_amount" id="asr_rate_amount" class="required" value="<?=isset($asr_result['asr_rate_amount']) ? ($asr_result['asr_rate_amount']) : null;?>" class="required number"/></td>
		</tr>
		<tr>
			<td><label><strong>Effective Start Date* :</label></strong></td>
			<td><input type="text" name="asr_rate_eff_start_date" id="from" class="required asr_rate_eff_start_date" value="<?=$asr_result['asr_rate_eff_start_date']?>"/></td>
		</tr>
		<tr>
			<td><label><strong>Effective End Date* :</label></strong></td>
			<td><input type="text" name="asr_rate_eff_end_date" id="to" class="required asr_rate_eff_end_date" value="<?=$asr_result['asr_rate_eff_end_date']?>"/></td>
		</tr>
		<tr>
			<td>&nbsp;<input type="hidden" value="asr_rate" name="asr_rate" />
			<input type="hidden" name="asrhidden" value="<?=$rtID;?>" id="asrid"/></td>
			<td><input type="submit" value="submit" name="submit" class="asrsubmit"/>
			<input type="button" value="Cancel" name="cancel" onclick="location.href='index.php?page=company-mang<?if(!$rtID){?>#tabs-3<?}?>'"/>
			</td>
		</tr>
		</table>
	</form>
</div>
</fieldset>